import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

import oracle.jdbc.OracleResultSet;
import oracle.ord.im.OrdImage;


public class DatabaseProcedures {
	
	/*void getData(Connection conn) {
        try {
            Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet result = statement.executeQuery("select * from obrazki");
            
            while (result.next()) {
            	System.out.println(result.getInt("id_obrazka"));
            	System.out.println(result.getString("nazwa_pliku"));
            	result.getBinaryStream("obrazek");
            }
            
            result.close();
            statement.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }*/
    
    Image showImage(Connection conn, int id) throws SQLException, IOException {
        Image img = null;
        try {
            Statement statement = conn.createStatement();
            OracleResultSet orclRS = (OracleResultSet)statement.executeQuery("select obrazek from obrazki where id_obrazka = " + id + "");
            
            if (orclRS.next()) {
                OrdImage ordImg = (OrdImage) orclRS.getORAData("obrazek", OrdImage.getORADataFactory());
                InputStream inputStream = ordImg.getContent().getBinaryStream();
                img = ImageIO.read(inputStream);
            }
            else System.out.println("ORDImage Error!");
            
            orclRS.close();
            statement.close();
        } catch (Exception e) {
        	System.out.println ("Could not load img from database... -.-");
        }
        return img;
    }
    
   /* void addNew(Connection conn, String n, String c, Double p, Double a, String l, String cr, String ct, String f) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call new_country('" + n + "', '" + c + "', "
                                                                  + p + ", " + a + ", '" + l + "', '" +  cr
                                                                  + "', '" + ct + "', '" + f + "') }");
            callStatement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseProcedures.class.getName()).log(Level.SEVERE, null, ex);
        }
    }*/
    
    void scaleImage(Connection conn, int id, double f) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call scale_image(" + id + ", " + f + ") }");
            callStatement.execute();
            conn.commit();
            System.out.println("Image has been resized. ;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void changeContrast(Connection conn, int id, double v) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call change_contrast(" + id + ", " + v + ") }");
            callStatement.execute();
            conn.commit();
            System.out.println("Contrast has been changed.;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void convertFileFormat(Connection conn, int id, String f) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call convert_file_format(" + id + ", '" + f + "') }");
            callStatement.execute();
            conn.commit();
            System.out.println("File format changed to " + f + ".;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void rotateImage(Connection conn, int id, double a) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call rotate_image(" + id + ", " + a + ") }");
            callStatement.execute();
            conn.commit();
            System.out.println("Image has been rotated by " + a + " degrees. ;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void flipImage(Connection conn, int id) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call flip_image(" + id + ") }");
            callStatement.execute();
            System.out.println("Image has been flipped. ;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void mirrorImage(Connection conn, int id) {
        try {conn.commit();
            CallableStatement callStatement = conn.prepareCall("{ call mirror_image(" + id + ") }");
            callStatement.execute();
            System.out.println("Image has been mirrored. ;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    void exportImage(Connection conn, int id, String n) {
        try {
            CallableStatement callStatement = conn.prepareCall("{ call export_image(" + id + ", '" + n + "') }");
            callStatement.execute();
            System.out.println("Image has been exported to C:\\MEDIA\\. ;)");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
}
